/**
 * amis v6.13.0
 * build time: 2025-08-12
 * Copyright 2018-2025 baidu
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
var _J$F_ = (__react_jsx__["default"] || __react_jsx__).Fragment;


;function VirtualTableBody(props) {
  var className = props.className,
    rows = props.rows,
    store = props.store,
    classPrefix = props.classPrefix;
  var leadingPlaceholderRef = React__default["default"].useRef(null);
  var trailingPlaceholderRef = React__default["default"].useRef(null);
  var tBodyRef = React__default["default"].useRef(null);
  var _a = tslib.__read(React__default["default"].useState(0), 2),
    scrollTop = _a[0],
    setScrollTop = _a[1];
  var itemHeight = React__default["default"].useRef(44);
  var sizeRef = React__default["default"].useRef(20);
  var buffer = 10;
  var offset = Math.floor(scrollTop / itemHeight.current);
  var start = Math.max(0, offset - buffer);
  var visibleRows = React__default["default"].useMemo(function () {
    return rows.slice(start, offset + sizeRef.current + buffer);
  }, [rows, offset, start, buffer]);
  React__default["default"].useEffect(function () {
    var tbody = tBodyRef.current;
    var table = tbody.parentElement;
    var wrap = table.parentElement;
    var rootDom = wrap.closest(".".concat(classPrefix, "Table"));
    var header = (rootDom === null || rootDom === void 0 ? void 0 : rootDom.querySelector(":scope > .".concat(classPrefix, "Table-fixedTop"))) || table.querySelector(':scope > thead');
    var firstRow = leadingPlaceholderRef.current;
    var isAutoFill = rootDom.classList.contains("".concat(classPrefix, "Table--autoFillHeight"));
    var toDispose = [];
    var check = function () {
      var rect = header.getBoundingClientRect();
      var rect2 = firstRow.getBoundingClientRect();
      var scrollTop = rect.bottom - rect2.top;
      setScrollTop(scrollTop);
      if (scrollTop && store.tableLayout !== 'fixed') {
        store.switchToFixedLayout();
      }
    };
    var timer = null;
    var lazyCheck = function () {
      timer && cancelAnimationFrame(timer);
      timer = requestAnimationFrame(check);
    };
    if (isAutoFill) {
      wrap.addEventListener('scroll', lazyCheck);
      toDispose.push(function () {
        return wrap.removeEventListener('scroll', lazyCheck);
      });
    } else {
      var scrollContainer_1 = amisCore.getScrollParent(rootDom);
      scrollContainer_1 = scrollContainer_1 === document.body ? document : scrollContainer_1;
      scrollContainer_1.addEventListener('scroll', lazyCheck);
      toDispose.push(function () {
        return scrollContainer_1.removeEventListener('scroll', lazyCheck);
      });
    }
    toDispose.push(amisCore.resizeSensor(wrap, function () {
      itemHeight.current = tbody.querySelector(':scope > tr').getBoundingClientRect().height;
      sizeRef.current = Math.min(Math.ceil(Math.min(isAutoFill ? wrap.clientHeight : window.innerHeight) / itemHeight.current), 20);
      check();
    }));
    return function () {
      toDispose.forEach(function (fn) {
        return fn();
      });
      toDispose.length = 0;
    };
  }, []);
  var styles = {
    '--Table-scroll-height': "".concat(itemHeight.current * rows.length, "px"),
    '--Table-scroll-offset': "".concat(start * itemHeight.current, "px"),
    '--Table-frame-height': "".concat(itemHeight.current * visibleRows.length, "px")
  };
  return _J$X_(React__default["default"].Fragment, null, _J$X_("tbody", {
    style: styles,
    className: "virtual-table-body-placeholder leading"
  }, _J$X_("tr", null, _J$X_("td", {
    colSpan: store.filteredColumns.length
  }, _J$X_("div", {
    ref: leadingPlaceholderRef
  })))), _J$X_("tbody", {
    className: className,
    ref: tBodyRef
  }, visibleRows), _J$X_("tbody", {
    style: styles,
    className: "virtual-table-body-placeholder trailing"
  }, _J$X_("tr", null, _J$X_("td", {
    colSpan: store.filteredColumns.length
  }, _J$X_("div", {
    ref: trailingPlaceholderRef
  })))));
}
var VirtualTableBody$1 = amisCore.themeable(VirtualTableBody);

exports["default"] = VirtualTableBody$1;
